package net.begincode.common.log;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;

import javax.servlet.*;
import java.io.IOException;
import java.util.UUID;

public class LogFilter implements Filter {
    private static final Logger LOGGER = LoggerFactory.getLogger(LogFilter.class);

    public void init(FilterConfig filterConfig) throws ServletException {

    }

    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
            throws IOException, ServletException {
        String uuid = UUID.randomUUID().toString();

        MDC.put("BeginTrace", uuid);
        try {
            filterChain.doFilter(servletRequest, servletResponse);
        }catch (Exception e){
            LOGGER.error("系统异常",e);
        } finally {
            MDC.clear();
        }
    }

    public void destroy() {

    }
}
